<template>
    <div class="login_container">
        <div class="login_box">
            <!-- 头像区域 -->
            <div class="avatar_box">
                <img src="../assets/weblogo.jpg" alt="" />
            </div>
            <!-- 表单区域 -->
            <el-form :model="loginFrom" ref="loginFormRef" :rules="loginFormRules" label-width="0px" class="login_form"
                v-loading="loading">
                <el-form-item prop="username"><!-- 用户名 -->
                    <el-input v-model="loginFrom.username" placeholder="请设置用户名"
                        prefix-icon="el-icon-user-solid"></el-input>
                </el-form-item>
                <el-form-item prop="password"><!-- 密码 -->
                    <el-input v-model="loginFrom.password" placeholder="请设置密码" prefix-icon="el-icon-lock"
                        type="password"></el-input>
                </el-form-item>
                <el-form-item prop="phone"><!-- 电话 -->
                    <el-input v-model="loginFrom.phone" placeholder="请绑定手机号" prefix-icon="el-icon-phone"></el-input>
                </el-form-item>
                <el-form-item prop="checkcode"><!-- 验证码 -->
                    <el-input style="width: 66%" placeholder="请输入验证码" v-model="loginFrom.checkcode"
                        prefix-icon="el-icon-menu"></el-input>
                    <el-button class="mybtn" @click="sendcode" type="info">{{ content }}</el-button>
                </el-form-item>
                <el-form-item class="btns"><!-- 按钮区域 -->
                    <el-button class="mybtn" @click="login" type="info">注册</el-button>
                    <el-button class="mybtn" @click="resetLoginForm" type="info">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>
   
<script>
export default {
    data() {
        return {
            //这里是登录表单的数据绑定对象
            loginFrom: {
                username: '',
                password: '',
                phone: '',
                checkcode: '',
            },
            //表单验证规则对象
            loginFormRules: {
                //验证用户名是否合法
                username: [
                    { required: true, message: '请输入账号', trigger: 'blur' },
                    { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
                ],
                //验证密码是否合法
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
                ],
                phone: [
                    { required: true, message: '请输入电话号码', trigger: 'blur' },
                    { min: 3, max: 20, message: '长度在 3 到 15 个字符', trigger: 'blur' }
                ]
            },
            loading: false,
            content: '发送验证码',  // 按钮里显示的内容
            totalTime: 60,      //记录具体倒计时时间
            canClick: true, //添加canClick
            ischeckcode: false,
            messageid: '',
        };
    },
    methods: {
        resetLoginForm() {
            this.$refs.loginFormRef.resetFields();
        },
        async sendcode() {
            if (!this.canClick) return  //改动的是这两行代码

            const res = await this.$http.get("user/sms", { params: { number: this.loginFrom.phone } });
            console.log(res);
            this.messageid = res.data.data.messageId;
            this.canClick = false;
            this.content = this.totalTime + 's后重新发送'
            let clock = window.setInterval(() => {
                this.totalTime--
                this.content = this.totalTime + 's后重新发送'
                if (this.totalTime < 0) {
                    window.clearInterval(clock)
                    this.content = '重发验证码'
                    this.totalTime = 10
                    this.canClick = true  //这里重新开启
                }
            }, 1000)

        },
        login() {
            this.$refs.loginFormRef.validate(async (valid) => {
                if (!valid) return;
                //   // console.log(this.loginFrom);
                let param = new URLSearchParams()
                param.append('username', this.loginFrom.username)
                param.append('password', this.loginFrom.password)
                param.append('tel', this.loginFrom.phone)
                param.append('checkcode', this.loginFrom.checkcode)
                param.append('messageId', this.messageid)
                const res = await this.$http.post("user/insertUserBean",param);
                console.log(res);
                //   if (res.data.status) return this.$message.error(res.data.msg);
                //   this.$data.loading = true;
                //   this.$message.success(res.data.msg);
                //   // 保存用户信息
                //   window.localStorage.setItem("token", res.data.data);
                //   window.sessionStorage.setItem("user", res.data.data.username);
                //   // console.log(res.data.data.user_id);
                //   window.sessionStorage.setItem("userid", res.data.data.user_id);
                // 登录跳转
                this.$router.push("/login");
            });
        },
    }
}
</script>
   
<style lang="less" scoped>
.login_container {
    background-color: #5a8cbb;
    height: 100%;
}

.login_box {
    width: 450px;
    height: 395px;
    background-color: #fff;
    border-radius: 3px; //圆角效果
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    background: rgba(255, 255, 255, 0.2);
    -webkit-backdrop-filter: blur(8px);
    backdrop-filter: blur(8px);
    border-radius: 10px;
    box-shadow: inset 0 0 6px rgba(255, 255, 255, 0.2);

    //css 嵌套
    .avatar_box {
        height: 130px;
        width: 130px;
        background-color: #fff;
        border: 1px solid #eee; //加个灰色边框
        border-radius: 50%; //div组件变成圆形
        padding: 10px; //图片和边框之间有10px间隙
        box-shadow: 0 0 10px #ddd; //添加一个阴影，向外扩散10像素
        //头像居中，向左移动box的50%，再向右移动自身的-50%，向上移动该组件高度的-50%，
        position: absolute;
        left: 50%;
        transform: translate(-50%, -50%);

        img {
            height: 100%;
            width: 100%;
            border-radius: 50%; //图片变成圆形
            background-color: #eee;
        }
    }
}

.login_form {
    position: absolute;
    bottom: 0px; //靠下
    width: 100%;
    padding: 0 20px; //上下内边距0，左右内边距20px
    //border-box为元素指定的任何内边距和边框都将在已设定的宽度和高度内进行绘制。
    //实际宽度会是width减去border + padding的计算值
    //此处是为了不让input超出我们的登录父div
    box-sizing: border-box;

}

.btns {
    display: flex; //flex布局
    //justify-content控制项目在横轴的对齐方式
    //flex-start即左对齐，center 为居中，对应的flex-end为右对齐。
    justify-content: flex-end;

}

.mybtn {
    background: rgba(255, 255, 255, 0.2);
    -webkit-backdrop-filter: blur(8px);
    backdrop-filter: blur(8px);
    border-radius: 10px;
    box-shadow: inset 0 0 6px rgba(255, 255, 255, 0.2);
}
</style>